package org.pps.gl.client.vue;

import org.pps.gl.client.LoginInfo;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.History;

/**
 * Page d'accueil du site avec une image du réseau Polytech
 * 
 */
public class Accueil extends Composite implements ValueChangeHandler<String> {
	DockLayoutPanel dockPanel;
	ScrollPanel flux;
	ACL_Messages vmessagerie;
	ACL_News vnews;
	ACL_Demande vdemandes;

	public Accueil() {
		
		dockPanel = new DockLayoutPanel(Unit.PX);
		// dockPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		initWidget(dockPanel);
		
		flux = new ScrollPanel();
		
		Image image = new Image("images/logos/PolytechReseau.png");
		image.setAltText("reseau-polytech");
		image.setSize("498px", "613px");
		
		flux.add(image);
		flux.setSize("98%", "100%");
		dockPanel.add(flux);
		// dockPanel.setCellHorizontalAlignment(image,
		// HasHorizontalAlignment.ALIGN_CENTER);
		dockPanel.setSize("100%", "100%");
		// initWidget(dockPanel);
	}

	/**
	 * Une fois connecté, la page est modifiée, on affiche les news
	 */
	public void setLoggedIn() {
		dockPanel.clear();

		// initWidget(dockPanel);
		dockPanel.setSize("100%", "100%");
		
		Integer nMess = LoginInfo.getCurrentUser().getUserInfo().getMessages().size();
		Integer nRec = LoginInfo.getCurrentUser().getReceived().size();
		Integer nDem = LoginInfo.getCurrentUser().getDemandes().size();
		Integer nNews = LoginInfo.getCurrentUser().getUserInfo().getNews().size();
		
		VerticalPanel menu = new VerticalPanel();

		menu.setSize("100%", "");
		menu.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		Hyperlink news = new Hyperlink("News ("+nNews+")", "news");
		news.setStylePrimaryName("menuHyper");
		Hyperlink messagerie = new Hyperlink("Messagerie ("+nMess+")", "messagerie");
		messagerie.setStylePrimaryName("menuHyper");
		Hyperlink demande = new Hyperlink("Demande ("+nRec+"/"+nDem+")", "demande");
		demande.setStylePrimaryName("menuHyper");

		menu.add(news);
		menu.add(messagerie);
		menu.add(demande);

		SimplePanel p = new SimplePanel();
		p.add(menu);
		p.setStylePrimaryName("navigation");
		p.setSize("153px", "100%");
		flux = new ScrollPanel();

		vmessagerie = new ACL_Messages();
		vnews = new ACL_News();
		vdemandes = new ACL_Demande();
		flux.add(vnews);
		flux.setSize("98%", "100%");
		History.addValueChangeHandler(this);

		dockPanel.addWest(p, 153);
		dockPanel.add(flux);
		dockPanel.setSize("100%", "100%");
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {

		if (event.getValue().equals("messagerie")) {
			flux.clear();
			flux.add(vmessagerie);
		}
		if (event.getValue().equals("news")) {
			flux.clear();
			flux.add(vnews);
		}
		if (event.getValue().equals("demande")) {
			flux.clear();
			flux.add(vdemandes);
		}
	}
}
